package com.industrial.operation.service.impl;

import com.industrial.operation.domain.OperationBenefitEvaluation;
import com.industrial.operation.domain.OperationCompanyInfo;
import com.industrial.operation.domain.PreMuJszxHy;
import com.industrial.operation.mapper.CompanyHxMapper;
import com.industrial.operation.service.ICompanyHxService;
import com.industrial.operation.utils.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ICompanyHxServiceImpl implements ICompanyHxService {

    @Autowired
    private CompanyHxMapper companyHxMapper;


    /**
     * 查询企业的信息
     *
     * @param companyId
     * @return
     */
    @Override
    public OperationCompanyInfo queryCompanyInfo(String companyId) throws IOException {
        OperationCompanyInfo preMuCompanyInfo = companyHxMapper.queryCompanyInfo(companyId);
        if (StringUtils.isNotBlank(preMuCompanyInfo.getDetailAddr())) {
            Map lonAndLat = MapUtils.getLonAndLat(preMuCompanyInfo.getDetailAddr());
            preMuCompanyInfo.setLonAndLat(lonAndLat);
        }
        return preMuCompanyInfo;
    }

    /**
     * 查询税务数据
     *
     * @param companyId
     * @return
     */
    @Override
    public List<OperationBenefitEvaluation> queryShuiWuData(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryShuiWuData(companyId);
        return result;
    }

    /**
     * 查询单位用地税收
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryDwydShuiShow(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryDwydShuiShow(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal score1 = item.getScore1();
            yAxis.add(score1);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询单位能耗销售收入
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryDwnhXssr(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryDwnhXssr(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal score2 = item.getScore2();
            yAxis.add(score2);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询单位污染物排放销售收入
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryDwwrwpfXssr(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryDwwrwpfXssr(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal score3 = item.getScore3();
            yAxis.add(score3);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询研发经费投入强度
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryYfjftrqd(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryYfjftrqd(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal score4 = item.getScore4();
            yAxis.add(score4);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询全员劳动生产率
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryQyldscl(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryQyldscl(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal score5 = item.getScore5();
            yAxis.add(score5);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询单位用地销售收
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryDwydxssr(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryDwydxssr(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal score6 = item.getScore6();
            yAxis.add(score6);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询生态环境数据
     *
     * @param operationBenefitEvaluation
     * @return
     */
    @Override
    public List<Map<String,Object>> querySthjsj(OperationBenefitEvaluation operationBenefitEvaluation) {
        OperationBenefitEvaluation result = companyHxMapper.querySthjsj(operationBenefitEvaluation);
        List<Map<String,Object>> resultList = new ArrayList<>();
        if(result == null){
            return resultList;
        }



        //化学需痒量
        String o2 = result.getO2();
        Map<String,Object> o2Map = new HashMap<>(16);
        o2Map.put("name","化学需痒量(COD)");
        o2Map.put("value",StringUtils.isNotBlank(o2) ? new BigDecimal(o2) : BigDecimal.ZERO);
        resultList.add(o2Map);

        //氨氮
        String ammonia = result.getAmmonia();
        Map<String,Object> ammoniaMap = new HashMap<>(16);
        ammoniaMap.put("name","氨氮(NH3-N)");
        ammoniaMap.put("value",StringUtils.isNotBlank(ammonia) ? new BigDecimal(ammonia) : BigDecimal.ZERO);
        resultList.add(ammoniaMap);
        //二氧化硫
        String so2 = result.getSo2();
        Map<String,Object> so2Map = new HashMap<>(16);
        so2Map.put("name","二氧化硫(SO2)");
        so2Map.put("value",StringUtils.isNotBlank(so2) ? new BigDecimal(so2) : BigDecimal.ZERO);
        resultList.add(so2Map);
        //氮氧化物
        String nox = result.getNox();
        Map<String,Object> noxMap = new HashMap<>(16);
        noxMap.put("name","氮氧化物(NOX)");
        noxMap.put("value",StringUtils.isNotBlank(nox) ? new BigDecimal(nox) : BigDecimal.ZERO);
        resultList.add(noxMap);
        //排放总当量
        String totalEquivalent = result.getTotalEquivalent();
        Map<String,Object> totalEquivalentMap = new HashMap<>(16);
        totalEquivalentMap.put("name","排放总当量(吨)");
        totalEquivalentMap.put("value",StringUtils.isNotBlank(totalEquivalent) ? new BigDecimal(totalEquivalent) : BigDecimal.ZERO);
        resultList.add(totalEquivalentMap);
        return resultList;
    }

    /**
     * 查询自然资源和规划数据
     *
     * @param operationBenefitEvaluation
     * @return
     */
    @Override
    public List<Map<String, Object>> queryzyzygh(OperationBenefitEvaluation operationBenefitEvaluation) {
        OperationBenefitEvaluation result = companyHxMapper.queryzyzygh(operationBenefitEvaluation);

        List<Map<String,Object>> resultList = new ArrayList<>();

        //自有使用权土地面积亩
        if(result != null){
            String userArea = result.getUseArea();
            Map<String,Object> userAreaMap = new HashMap<>(16);
            userAreaMap.put("name","自有使用权土地面积亩");
            userAreaMap.put("value",StringUtils.isNotBlank(userArea) ? new BigDecimal(userArea) : BigDecimal.ZERO);
            resultList.add(userAreaMap);

            //其他实际占用面积亩
            String otherArea = result.getOtherArea();
            Map<String,Object> otherAreaMap = new HashMap<>(16);
            otherAreaMap.put("name","其他实际占用面积亩");
            otherAreaMap.put("value",StringUtils.isNotBlank(otherArea) ? new BigDecimal(otherArea) : BigDecimal.ZERO);
            resultList.add(otherAreaMap);
            //租赁面积
            String leasedArea = result.getLeasedArea();
            Map<String,Object> leasedAreaMap = new HashMap<>(16);
            leasedAreaMap.put("name","租赁面积");
            leasedAreaMap.put("value",StringUtils.isNotBlank(leasedArea) ? new BigDecimal(leasedArea) : BigDecimal.ZERO);
            resultList.add(leasedAreaMap);
            //对外出租面积亩
            String rentOutArea = result.getRentOutArea();
            Map<String,Object> rentOutAreaMap = new HashMap<>(16);
            rentOutAreaMap.put("name","对外出租面积亩");
            rentOutAreaMap.put("value",StringUtils.isNotBlank(rentOutArea) ? new BigDecimal(rentOutArea) : BigDecimal.ZERO);
            resultList.add(rentOutAreaMap);
            //用地面积合计
            BigDecimal sumArea = result.getSumArea();
            Map<String,Object> sumAreaMap = new HashMap<>(16);
            sumAreaMap.put("name","用地面积合计");
            sumAreaMap.put("value",sumArea != null ? sumArea : BigDecimal.ZERO);
            resultList.add(sumAreaMap);
        }
        return resultList;
    }

    /**
     * 查询企业工业总产值
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String,Object>  queryGyzcz(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryGyzcz(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal industryOutput = item.getIndustryOutput();
            yAxis.add(industryOutput);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询企业总能耗
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryZnh(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryZnh(companyId);

        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            String totalUsed = item.getTotalUsed();
            yAxis.add(totalUsed);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询企业用地面积
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryYdmj(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryYdmj(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal totalUsed = item.getSumArea();
            yAxis.add(totalUsed);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询企业污染排放量
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> querwrpfl(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.querwrpfl(companyId);

        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            String totalUsed = item.getTotalEquivalent();
            yAxis.add(totalUsed);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询人力资源和社会保障数据
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryRlzyhshbz(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryRlzyhshbz(companyId);

        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            Long annualEmployees = item.getAnnualEmployees();
            yAxis.add(annualEmployees);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询水力数据
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryShuili(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryShuili(companyId);

        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            Long annualEmployees = item.getAnnualEmployees();
            yAxis.add(annualEmployees);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询耗煤量
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryHml(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryHml(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal coalConsumption = item.getCoalConsumption();
            yAxis.add(coalConsumption);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询耗电量
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryHdl(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryHdl(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal powerConsumption = item.getPowerConsumption();
            yAxis.add(powerConsumption);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 查询耗起量
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryHql(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryHql(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal gasConsumption = item.getGasConsumption();
            yAxis.add(gasConsumption);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 加载该企业存在数据的年份
     *
     * @param companyId
     * @return
     */
    @Override
    public List<String> loadCompanyExistDataYear(String companyId) {
        List<String> result = companyHxMapper.loadCompanyExistDataYear(companyId);
        return result;
    }

    /**
     * 查询研发经费支出
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Object> queryyfjfzc(String companyId) {
        List<OperationBenefitEvaluation> result = companyHxMapper.queryyfjfzc(companyId);

        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationBenefitEvaluation item : result){
            String years = item.getYears();
            xAxis.add(years);

            BigDecimal gasConsumption = item.getRdFunds();
            yAxis.add(gasConsumption);
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * @param operationCompanyInfo
     * @return
     */
    @Override
    public List<OperationCompanyInfo> list(OperationCompanyInfo operationCompanyInfo) {
        List<OperationCompanyInfo> result = companyHxMapper.list(operationCompanyInfo);
        return result;
    }

    /**
     * @return
     */
    @Override
    public List<PreMuJszxHy> loadIndustryType() {
        List<PreMuJszxHy> result = companyHxMapper.loadIndustryType();
        return result;
    }

    /**
     * 查询外层表的年份
     *
     * @return
     */
    @Override
    public List<String> queryQcTableYear() {
        List<String> result = companyHxMapper.queryQcTableYear();
        return result;
    }

    /**
     * 查询企业行业的报表
     *
     * @return
     */
    @Override
    public Map<String, Object> queryQyHyCharts(OperationCompanyInfo operationCompanyInfo) {
        List<OperationCompanyInfo> operationCompanyInfos = companyHxMapper.queryQyHyCharts(operationCompanyInfo);
        List<String> xAix = new ArrayList<>();
        List<Long> yAix = new ArrayList<>();
        for(OperationCompanyInfo temp : operationCompanyInfos){
            String industryType = temp.getIndustryType();
            xAix.add(industryType);

            Long count = temp.getCount();
            yAix.add(count);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("xAix",xAix);
        result.put("yAix",yAix);
        return result;
    }
}
